package com.cyandpos.pad.utils.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.Coupon;
import com.cyandpos.pad.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

public class CouponMessageDialogCommon extends AlertDialog {
    private static  CouponMessageDialogCommon couponMessageDialogCommon;
    private Context context;
    private View view;
    private TextView tv_title;
    private ImageView iv_close;
    private Button bt_submit;
    private Button bt_cancle;
    private TextView tv_title_one;
    private TextView tv_title_two;
    private MyListView listview_coupon;
    private MyListView listview_coupon_two;
    private CouponMessageDialogCommonOnClickListener couponMessageDialogCommonOnClickListener;
    private List<Coupon> coupons;
    private List<Coupon> couponsTwo;
    private  CouponMessageDialogCommon(Context context,List<Coupon> coupons,List<Coupon> couponsTwo, final CouponMessageDialogCommonOnClickListener couponMessageDialogCommonOnClickListener) {
        super(context);
        this.context=context;
        this.coupons=coupons;
        this.couponsTwo=couponsTwo;
        this.couponMessageDialogCommonOnClickListener=couponMessageDialogCommonOnClickListener;
        view = LayoutInflater.from(context).inflate(R.layout.dialog_coupon_message_common,null);
        tv_title=view.findViewById(R.id.tv_title);
        tv_title_one=view.findViewById(R.id.tv_title_one);
        tv_title_two=view.findViewById(R.id.tv_title_two);
        listview_coupon=view.findViewById(R.id.listview_coupon);
        listview_coupon_two=view.findViewById(R.id.listview_coupon_two);
        iv_close=view.findViewById(R.id.iv_close);
        bt_submit=view.findViewById(R.id.bt_submit);
        bt_cancle=view.findViewById(R.id.bt_cancle);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                couponMessageDialogCommon.dismiss();
                couponMessageDialogCommon=null;
                if(couponMessageDialogCommonOnClickListener!=null){
                    couponMessageDialogCommonOnClickListener.couponMessageDialogCommonOnClick();
                }
            }
        });
        bt_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                couponMessageDialogCommon.dismiss();
                couponMessageDialogCommon=null;
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                couponMessageDialogCommon.dismiss();
                couponMessageDialogCommon=null;
            }
        });
        CouponAdapter couponAdapter=new CouponAdapter(context,coupons);
        CouponAdapter couponAdapter1=new CouponAdapter(context,couponsTwo);
        listview_coupon.setAdapter(couponAdapter);
        listview_coupon_two.setAdapter(couponAdapter1);
    }

    public static  CouponMessageDialogCommon getCouponMessageDialogCommon(Context context, List<Coupon> coupons,List<Coupon> couponsTwo,CouponMessageDialogCommonOnClickListener couponMessageDialogCommonOnClickListener){
        if(couponMessageDialogCommon==null){
            couponMessageDialogCommon=new CouponMessageDialogCommon(context,coupons,couponsTwo,couponMessageDialogCommonOnClickListener);
        }
        return couponMessageDialogCommon;
    }

    /**
     * 显示弹窗
     * @param
     * @param isClose
     */
    public  void showDialog(boolean isClose){
        couponMessageDialogCommon.setCancelable(isClose);
        couponMessageDialogCommon.setView(view,0,0,0,0);
        couponMessageDialogCommon.show();
        couponMessageDialogCommon.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg_common);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        if(tv_title!=null)
            tv_title.setText(title);
    }
    /**
     * 设置标题1
     * @param title
     */
    public void setTitleOne(String title){
        if(tv_title_one!=null)
            tv_title_one.setText(title);
    }
    /**
     * 设置标题2
     * @param title
     */
    public void setTitleTwo(String title){
        if(tv_title_two!=null)
            tv_title_two.setText(title);
    }
    /**
     * 隐藏标题1
     * @param
     */
    public void setTitleOneGone(boolean isGone){
        if(isGone){
            if(tv_title_one!=null)
                tv_title_one.setVisibility(View.GONE);
        }else {
            if (tv_title_one != null)
                tv_title_one.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏标题2
     * @param
     */
    public void setTitleTwoGone(boolean isGone){
        if(isGone){
            if(tv_title_two!=null)
                tv_title_two.setVisibility(View.GONE);
        }else {
            if (tv_title_two != null)
                tv_title_two.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 隐藏列表1
     * @param
     */
    public void setListOneGone(boolean isGone){
        if(isGone){
            if(listview_coupon!=null)
                listview_coupon.setVisibility(View.GONE);
        }else {
            if (listview_coupon != null)
                listview_coupon.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 隐藏列表2
     * @param
     */
    public void setListTwoeGone(boolean isGone){
        if(isGone){
            if(listview_coupon_two!=null)
                listview_coupon_two.setVisibility(View.GONE);
        }else {
            if (listview_coupon_two != null)
                listview_coupon_two.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏取消按钮
     * @param
     */
    public void setBt_cancle(boolean isGone){
        if(isGone){
            if(bt_cancle!=null)
                bt_cancle.setVisibility(View.GONE);
        }else {
            if (bt_cancle != null)
                bt_cancle.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏关闭按钮
     * @param
     */
    public void setIv_close(boolean isGone){
        if(isGone){
            if(iv_close!=null)
                iv_close.setVisibility(View.GONE);
        }else {
            if (iv_close != null)
                iv_close.setVisibility(View.VISIBLE);
        }
    }

    public interface CouponMessageDialogCommonOnClickListener{
        void couponMessageDialogCommonOnClick();
    }

    public class CouponAdapter extends BaseAdapter{
        private Context context;
        private List<Coupon> coupons;
        public CouponAdapter(Context mContext, List<Coupon> coupons) {
            this.context=mContext;
            this.coupons=coupons;
        }

        @Override
        public int getCount() {
            return coupons.size();
        }

        @Override
        public Coupon getItem(int position) {
            return coupons.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.list_show_coupon_item, parent, false);
                holder=new Holder();
                holder.mYhjmc=convertView.findViewById(R.id.tv_yhjmc_item);
                holder.mYhjje=convertView.findViewById(R.id.tv_yhjje_item);
                holder.llDeadline=convertView.findViewById(R.id.ll_deadLine_item);
                holder.mDeadLine=convertView.findViewById(R.id.tv_deadline);
                holder.tv_number_item=convertView.findViewById(R.id.tv_number_item);
                convertView.setTag(holder);
            }else{
                holder= (Holder) convertView.getTag();
            }
            Coupon coupon=getItem(position);
            if(coupon!=null){
                holder.mYhjmc.setText(coupon.getCouponTypeName());
                holder.mYhjje.setText(coupon.getAvailableAmount());
                if(!CommonUtils.isNullOrEmpty(coupon.getValidDate())){
                    holder.llDeadline.setVisibility(View.VISIBLE);
                    holder.mDeadLine.setText(coupon.getValidDate());
                }
                if(coupon.getCouponCode()!=null&&!"".equals(coupon.getCouponCode())&&!"null".equals(coupon.getCouponCode())) {
                    holder.tv_number_item.setText(coupon.getCouponCode());
                }else{
                    holder.tv_number_item.setText("无");
                }
            }
            return convertView;
        }
        class  Holder{
            private TextView mYhjmc;
            private  TextView mYhjje;
            private  LinearLayout llDeadline;
            private TextView mDeadLine;
            private TextView tv_number_item;
        }
    }
}

